﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using Vtron.HipcConfigurationTools.Domain;

namespace Vtron.HipcConfigurationTools.VWASStyle
{
    public class DataStore
    {
        /// <summary>
        /// Returns a list of all Codeces stored in the folder.xml file
        /// </summary>
        /// <param name="path">The path to the user'd directory</param>
        /// <returns>A list of folder paths</returns>
        public static List<CodecInfo> GetCodecListing()
        {
            string directoryPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Vtron\\HIPC Codec";
            string filePath = directoryPath + @"\codec.xml";

            List<CodecInfo> folderList = new List<CodecInfo>();

            if (File.Exists(filePath))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filePath);

                XmlNodeList xmlFolderList = doc.GetElementsByTagName("Codec");

                foreach (XmlNode node in xmlFolderList)
                {
                    XmlElement folderElement = (XmlElement)node;
                    CodecInfo codecInfo = new CodecInfo();
                    codecInfo.Index = Int32.Parse(folderElement.GetElementsByTagName("Index")[0].InnerText);
                    codecInfo.Ip = folderElement.GetElementsByTagName("Ip")[0].InnerText;
                    codecInfo.CodecType = (CodecType)Int32.Parse(folderElement.GetElementsByTagName("CodecType")[0].InnerText);
                    folderList.Add(codecInfo);
                }
                return folderList;
            }
            else
            {
                Directory.CreateDirectory(directoryPath);
                FileStream fs = File.Create(filePath);
                fs.Close();
                XmlDocument xmlDoc = new XmlDocument();
                XmlElement mainElement = xmlDoc.CreateElement("HIPCCodec");
                xmlDoc.AppendChild(mainElement);
                xmlDoc.Save(filePath);
            }
            return folderList;
        }

        /// <summary>
        /// Saves the current set of folders to disk
        /// </summary>
        /// <param name="folderList">A list of the folders this application is looking out for</param>
        public static void SaveFolderListing(List<CodecInfo> folderList)
        {
            string filePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Vtron\\HIPC Codec" + @"\codec.xml";

            XmlDocument xmlDoc = new XmlDocument();
            XmlElement mainElement = xmlDoc.CreateElement("HIPCCodec");

            foreach (CodecInfo folderPath in folderList)
            {
                XmlElement folderElement = xmlDoc.CreateElement("Codec");

                XmlElement IndexElement = xmlDoc.CreateElement("Index");
                IndexElement.InnerText = folderPath.Index.ToString();
                folderElement.AppendChild(IndexElement);

                XmlElement IpElement = xmlDoc.CreateElement("Ip");
                IpElement.InnerText = folderPath.Ip;
                folderElement.AppendChild(IpElement);

                XmlElement CodecTypeElement = xmlDoc.CreateElement("CodecType");
                CodecTypeElement.InnerText = ((byte)folderPath.CodecType).ToString();
                folderElement.AppendChild(CodecTypeElement);

                mainElement.AppendChild(folderElement);
            }

            xmlDoc.AppendChild(mainElement);
            xmlDoc.Save(filePath);
        }
    }
}
